【数据结构】之迷宫问题

所谓迷宫,就是在一个矩阵中,从开始位置有一条通路可以走到最末尾的位置

先画一个迷宫,格式为txt,和编译的文件夹放在一起

在迷宫中,0表示可以走通的路,而1则表示不可走通的墙

首先定义一个结构体,用来存放行和列

struct Pos
{
	size_t row;
	size_t col;
};
接下来从文件中获得迷宫 的矩阵

void GetMaze(int *maze, size_t N)
{
	FILE *fp = fopen("MazeMap.txt", "r");
	if (fp == NULL)
	{
		throw invalid_argument("Read maze filed");
	}

	for (size_t i = 0; i < N; ++i)
	{
		for (size_t j = 0; j < N;)
		{
			char ch = fgetc(fp);
			if (ch == '0' || ch == '1')
			{
				maze[i*N + j] = ch - '0';
			}
			else if (ch == EOF)
			{
				throw invalid_argument("Maze Error");
			}
		}
	}
}
然后就要判断迷宫中的每一个坐标的路是否为通路

bool CheckIsAccess(int* maze, size_t N, Pos next)
{
	if (maze[next.row*N + next.col] == 0)
	{
		return true;
	}
	return false;
}

接下俩对于路径的求解的算法有两种方法,递归和非递归

非递归求解:

bool GetMazePath(int *maze, size_t n, Pos entry, stack<Pos>&path)
{
	maze[entry.row*N + entry.col] = 2;
	Pos cur, next;
	cur = entry;
	path.push(entry);
	while (!path.empty())
	{
		cur = path.top();

		if (cur.row == N - 1)
		{
			return true;
		}
		next = cur;

		next.row += 1;
		if (CheckIsAccess((int*)maze, N, next) == true)
		{
			path.push(next);
			maze[next.row*N + next.col] = 2;
			continue;
		}
		next = cur;

		next.row -= 1;
		if (CheckIsAccess((int*)maze, N, next) == true)
		{
			path.push(next);
			maze[next.row*N + next.col] = 2;
			continue;
		}
		next = cur;

		next.col += 1;
		if (CheckIsAccess((int*)maze, N, next) == true)
		{
			path.push(next);
			maze[next.row*N + next.col] = 2;
			continue;
		}
		next = cur;

		next.col -= 1;
		if (CheckIsAccess((int*)maze, N, next) == true)
		{
			path.push(next);
			maze[next.row*N + next.col] = 2;
			continue;
		}
		next = cur;
		path.pop();
	}
	return false;
}

用递归的方法:

void GetMazePath_R(int *maze, size_t N,Pos entry, stack<Pos>&path,stack<Pos>&shortpath)
{
	if (entry.row == N - 1)
	{
		if (path.size() < shortpath.size() || shortpath.size() == 0)
		{
			shortpath = path;
		}
		path.pop();
		return;
	}
	Pos cur, next;
	cur = entry;
	next = cur;

	next.row += 1;
	if (CheckIsAccess((int*)maze, N, next, cur) == true)
	{
		path.push(next);
		maze[next.row*N + next.col] = maze[cur.row*N + cur.col] + 1;
		GetMazePath_R(maze, N, next, path, shortpath);
	}
	next = cur;

	next.row -= 1;
	if (CheckIsAccess((int*)maze, N, next, cur) == true)
	{
		path.push(next);
		maze[next.row*N + next.col] = maze[cur.row*N + cur.col] + 1;
		GetMazePath_R((int*)maze, N, next,path, shortpath);
	}

	next.col += 1;
	if (CheckIsAccess((int*)maze, N, next, cur) == true)
	{
		path.push(next);
		maze[next.row*N + next.col] = maze[cur.row*N + cur.col] + 1;
		GetMazePath_R((int*)maze, N, next, path, shortpath);
	}

	next.col -= 1;
	if (CheckIsAccess((int*)maze, N, next, cur) == true)
	{
		path.push(next);
		maze[next.row*N + next.col] = maze[cur.row*N + next.col] + 1;
		GetMazePath_R((int*)maze, N, next, path, shortpath);
	}

	path.pop();
}

其中的-=1和+=1是表示在一个位置处有四个方向,通过行和列的+1和-1来表示这四个方向。

下面就是完整的代码:

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
#include<stack>

const int N = 10;

struct Pos
{
	size_t row;
	size_t col;
};

void PrintMaze(int *maze, size_t N)
{
	for (size_t i = 0; i < N; ++i)
	{
		for (size_t j = 0; j < N; ++j)
		{
			cout << maze[i*N + j] << " ";
		}
		cout << endl;
	}
	cout << endl;
}

bool CheckIsAccess(int *maze, size_t N, Pos next)
{
	if (maze[next.row*N + next.col] == 0)
	{
		return true;
	}
	return false;
}

bool CheckIsAccess(int *maze, size_t N, Pos next, Pos cur)
{
	if (next.row < 0 || next.row >= N || next.col < 0 || next.col >= N)
	{
		return false;
	}

	if (maze[next.row*N + next.col] == 0)
	{
		return true;
	}
	return(maze[next.row*N + next.col]>(maze[cur.row*N + next.col] + 1));
}


void GetMaze(int *maze, size_t N)
{
	FILE *fp = fopen("MazeMap.txt", "r");
	if (fp == NULL)
	{
		throw invalid_argument("Read maze filed");
	}

	for (size_t i = 0; i < N; ++i)
	{
		for (size_t j = 0; j < N;)
		{
			char ch = fgetc(fp);
			if (ch == '0' || ch == '1')
			{
				maze[i*N + j] = ch - '0';
				++j;
			}
			else if (ch == EOF)
			{
				throw invalid_argument("Maze Error");
			}
		}
	}
}

bool GetMazePath(int *maze, size_t n, Pos entry, stack<Pos>&path)
{
	maze[entry.row*N + entry.col] = 2;
	Pos cur, next;
	cur = entry;
	path.push(entry);
	while (!path.empty())
	{
		cur = path.top();

		if (cur.row == N - 1)
		{
			return true;
		}
		next = cur;

		next.row += 1;
		if (CheckIsAccess((int*)maze, N, next) == true)
		{
			path.push(next);
			maze[next.row*N + next.col] = 2;
			continue;
		}
		next = cur;

		next.row -= 1;
		if (CheckIsAccess((int*)maze, N, next) == true)
		{
			path.push(next);
			maze[next.row*N + next.col] = 2;
			continue;
		}
		next = cur;

		next.col += 1;
		if (CheckIsAccess((int*)maze, N, next) == true)
		{
			path.push(next);
			maze[next.row*N + next.col] = 2;
			continue;
		}
		next = cur;

		next.col -= 1;
		if (CheckIsAccess((int*)maze, N, next) == true)
		{
			path.push(next);
			maze[next.row*N + next.col] = 2;
			continue;
		}
		next = cur;
		path.pop();
	}
	return false;
}


void GetMazePath_R(int *maze, size_t N,Pos entry, stack<Pos>&path,stack<Pos>&shortpath)
{
	if (entry.row == N - 1)
	{
		if (path.size() < shortpath.size() || shortpath.size() == 0)
		{
			shortpath = path;
		}
		path.pop();
		return;
	}
	Pos cur, next;
	cur = entry;
	next = cur;

	next.row += 1;
	if (CheckIsAccess((int*)maze, N, next, cur) == true)
	{
		path.push(next);
		maze[next.row*N + next.col] = maze[cur.row*N + cur.col] + 1;
		GetMazePath_R(maze, N, next, path, shortpath);
	}
	next = cur;

	next.row -= 1;
	if (CheckIsAccess((int*)maze, N, next, cur) == true)
	{
		path.push(next);
		maze[next.row*N + next.col] = maze[cur.row*N + cur.col] + 1;
		GetMazePath_R((int*)maze, N, next,path, shortpath);
	}

	next.col += 1;
	if (CheckIsAccess((int*)maze, N, next, cur) == true)
	{
		path.push(next);
		maze[next.row*N + next.col] = maze[cur.row*N + cur.col] + 1;
		GetMazePath_R((int*)maze, N, next, path, shortpath);
	}

	next.col -= 1;
	if (CheckIsAccess((int*)maze, N, next, cur) == true)
	{
		path.push(next);
		maze[next.row*N + next.col] = maze[cur.row*N + next.col] + 1;
		GetMazePath_R((int*)maze, N, next, path, shortpath);
	}

	path.pop();
}

void ReMaze(int* maze)
{
	for (size_t i = 0; i < N; ++i)
	{
		for (size_t j = 0; j < N; ++j)
		{
			if (maze[i*N + j] != 1)
			{
				maze[i*N + j] = 0;
			}
		}
	}
}

stack<Pos>MinPath()
{
	int maze[N][N] = { 0 };
	int curmaze[N][N] = { 0 };
	GetMaze((int*)maze, N);
	stack<Pos>path, MinPath, empty;

	while (GetMazePath((int*)maze, N, { 0, 0 }, path))
	{
		if (path.size() < MinPath.size()||MinPath.size() == 0)
		{
			MinPath = path;
		}

		ReMaze((int*)maze);
		maze[path.top().row][path.top().col] = 1;
		path = empty;
	}
	return MinPath;
}


void Test()
{
	try
	{
		int maze[N][N] = { 0 };
		int curmaze[N][N] = { 0 };
		GetMaze((int*)maze, N);
		stack<Pos>path, shortpath;
		path.push({ 0, 0 });
		GetMazePath_R((int*)maze, N, { 0, 0 }, path, shortpath);
		cout << shortpath.size() << endl;
	}
	catch (exception &e)
	{
		cout << e.what() << endl;
	}
}

int main()
{
	Test();
	system("pause");
	return 0;
}


 


  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 递归解决迷宫问题是一种常见的方法。在这种方法,我们使用递归函数来探索迷宫的每个可能的路径,直到我们找到出口或确定没有解决方案为止。 首先,我们需要定义一个迷宫。可以使用一个二维数组来表示迷宫,其 0 表示可以通过的位置,1 表示障碍物或墙。 接下来,我们需要定义一个递归函数,该函数将从当前位置开始探索迷宫。在每个位置上,我们检查是否已到达出口,如果是,则返回真。否则,我们尝试从当前位置向四个方向移动,并递归地调用该函数以继续探索。如果在任何方向上找到了出口,则返回真。如果没有找到出口,则返回假。 以下是一个可能的解决方案(使用 C++ 实现): ```cpp #include <iostream> using namespace std; const int N = 5; int maze[N][N] = { {0, 1, 0, 0, 0}, {0, 1, 0, 1, 0}, {0, 0, 0, 0, 0}, {0, 1, 1, 1, 0}, {0, 0, 0, 1, 0} }; bool solve(int x, int y) { if (x == N-1 && y == N-1) { // 到达出口 return true; } if (x < 0 || y < 0 || x >= N || y >= N) { // 超出边界 return false; } if (maze[x][y] == 1) { // 遇到障碍物 return false; } maze[x][y] = 1; // 标记为已访问 if (solve(x+1, y) || solve(x-1, y) || solve(x, y+1) || solve(x, y-1)) { return true; // 向四个方向递归查找 } maze[x][y] = 0; // 恢复为未访问 return false; // 没有找到出口 } int main() { if (solve(0, 0)) { cout << "迷宫有解!" << endl; } else { cout << "迷宫无解。" << endl; } return 0; } ``` 在上面的代码,我们首先从起点 (0, 0) 开始调用 solve() 函数。在 solve() 函数,我们按顺序检查当前位置是否为出口、是否超出边界、是否遇到障碍物。如果都不是,则标记当前位置为已访问,并向四个方向递归调用 solve() 函数以继续探索。如果在任何方向上找到出口,则返回真。如果没有找到出口,则恢复当前位置为未访问状态,并返回假。 在主函数,我们检查 solve() 函数的返回值以确定迷宫是否有解。如果有解,则输出“迷宫有解!”;否则,输出“迷宫无解。”。 需要注意的是,这种方法可能会导致栈溢出,因为每个递归调用都会将一个新的栈帧压入堆栈。为了避免这种情况,可以使用迭代加深搜索或其他更高级的搜索算法。 ### 回答2: 数据结构递归算法可以用于解决迷宫问题迷宫问题是在一个矩阵找到从起点到终点的路径。矩阵可以表示为一个二维数组,其0表示可通行的路径,1表示墙壁或障碍物。我们需要找到一条从起点到终点的路径,路径可以相邻的上下左右移动。 使用递归算法可以很方便地解决迷宫问题。我们可以从起点开始,每次选择一个方向前进,并通过递归调用来继续探索下一步是否可行。若当前位置为终点,则找到了一条路径,返回true。若当前位置已经走过或者是墙壁,则返回false。若当前位置是可通行路径,则继续向四个方向递归调用,直到找到一条可行路径或者所有方向都走过一遍。 通过递归算法,我们可以遍历所有可能的路径,直到找到一条通往终点的路径或者确定没有可行路径为止。算法的基本思路是不断向前探索并回溯,重复这个过程直到找到结果。 递归算法在解决迷宫问题时可以使用栈来保存之前的路径,并在回溯时将路径出栈,以便返回上一步。同时,为了避免重复探索,需要使用一个二维数组来记录每个位置是否已经走过。 总之,使用数据结构递归算法可以很好地解决迷宫问题。该算法的时间复杂度取决于迷宫的大小和难度,但在一般情况下是可以接受的。 ### 回答3: 数据结构的递归在解决迷宫问题非常有用。迷宫问题是一个典型的寻路问题,目标是从迷宫的起点找到一条通往终点的路径。 在数据结构,可以使用递归来解决这个问题。我们可以将迷宫看作是一个二维数组,每个元素表示一个迷宫的单元格。其,0代表可通过的通道,1代表墙壁或障碍物。 首先,我们需要定义一个递归函数,用来找到从当前位置到终点的路径。该函数的输入参数包括当前位置的行和列,以及表示迷宫的二维数组。递归的结束条件就是当前位置已经是终点,即到达了目标位置。 在每一步递归,我们需要检查当前位置的上、下、左、右四个方向是否可以移动。如果某个方向可以移动,我们就将下一步的位置设为该方向,并调用递归函数。如果递归函数返回真,表示找到了一条有效路径,我们就返回真;否则,我们需要回溯到上一步,并继续尝试其他方向。 在递归函数,还需要有一个标记数组来记录已经访问过的位置,避免重复访问。每次需要访问一个位置时,我们首先检查该位置是否超出了迷宫的边界,并且该位置没有被访问过。如果满足这两个条件,我们将该位置标记为已访问,并继续递归。 如果递归函数返回真,表示找到了一条有效路径,我们可以将该路径输出或保存下来。如果递归函数返回假,并且没有其他方向可以继续尝试,表示无法找到有效路径,我们就返回假。 数据结构的递归在解决迷宫问题上是非常高效的,因为它能够自动探索所有可能的路径,而不需要编写复杂的循环代码。通过合理的设计递归函数和标记数组,我们能够有效地解决迷宫问题,并找到一条通往终点的路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值